import axios from 'axios'
import { ElMessage } from 'element-plus'
import store from '@/store'
const server = axios.create({
  baseURL: 'http://8.148.65.61:7000/api',//'http://127.0.0.1:9001',
  timeout: 20000
})

server.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
})
server.interceptors.response.use(response => {
  const { code, message } = response.data
  if (code === 200) {
    return response.data
  } else {
    ElMessage({
      message: message,
      type: 'error',
      onClose: () => {
        if (response.data.code === 415) {
          // 若登录超时 或者 未登录，则执行登出操作
          store.dispatch('loginOut').then(res => {

          })
        }
      }
    })

    return response.data
  }
}, error => {
  ElMessage({
    message: '请求出错，请重试',
    type: 'error',
    duration: 1500
  })
  return error
})
export default server
